<?php

namespace app\admin\dao;

use app\common\model\Transaction;
use ShidianUtil\Thinkphp\Dao\DaoTrait;

/**
 * TransactionDao 数据访问对象
 * Class TransactionDao
 * @package app\admin\dao
 * @method Transaction findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method Transaction insertActive(array $params, array $allow_field = [])
 * @method Transaction updateActive(Transaction $model, array $params, array $allow_field = [])
 * @method Transaction findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method Transaction updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(Transaction $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class TransactionDao
{
    use DaoTrait;

    /**
     * @var Transaction
     */
    private $baseModel;

    /**
     * TransactionDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = Transaction::class;
    }

    public function listTransactionMargin($memberId, $startTime, $endTime, $page, $page_size): array
    {
        $query = Transaction::when(is_id($memberId), fn($query) => $query->where('memberId', $memberId))
            ->whereIn('accountType', [PAYMENT_MARGIN])
            ->when(is_numeric($startTime), fn($query) => $query->where('created', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('created', '<=', $endTime))
            ->with([
                'user' => fn($query) => $query->field(['id', 'name']),
                'member' => fn($query) => $query->shortMember(),
            ])
            ->order("id", 'DESC')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }


    public function listTransactionOfCompany($companyId, $startTime, $endTime, $page, $page_size): array
    {
        $query = Transaction::when(is_id($companyId), fn($query) => $query->where('companyId', $companyId))
            ->where('companyId', '<>', '0')
            ->whereIn('accountType', [PAYMENT_CARD, PAYMENT_COIN])
            ->when(is_numeric($startTime), fn($query) => $query->where('created', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('created', '<=', $endTime))
            ->with([
                'user' => fn($query) => $query->field(['id', 'name']),
                'company' => fn($query) => $query->field(['id', 'name']),
            ])
            ->order("id", 'DESC')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }

    public function statisticsListTransactionOfCompany($companyId, $startTime, $endTime): array
    {
        $query = Transaction::when(is_id($companyId), fn($query) => $query->where('companyId', $companyId))
            ->where('companyId', '<>', '0')
            ->whereIn('accountType', [PAYMENT_CARD, PAYMENT_COIN])
            ->when(is_numeric($startTime), fn($query) => $query->where('created', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('created', '<=', $endTime));

        return [
            'total_count' => $query->count(),
            'amount_sum' => $query->sum('amount')
        ];
    }

    public function listTransactionOfMember($startTime, $endTime, $page, $page_size): array
    {
        $query = Transaction::where('memberId', '<>', '0')
            ->whereIn('accountType', [PAYMENT_CARD, PAYMENT_COIN])
            ->when(is_numeric($startTime), fn($query) => $query->where('created', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('created', '<=', $endTime))
            ->with([
                'user' => fn($query) => $query->field(['id', 'name']),
                'member' => fn($query) => $query->shortMember(),
            ])
            ->order("id", 'DESC')
            ->paging($page, $page_size);

        return listOutput($query, $page_size);
    }

    public function statisticsListTransactionOfMember($startTime, $endTime): array
    {
        $query = Transaction::where('memberId', '<>', '0')
            ->whereIn('accountType', [PAYMENT_CARD, PAYMENT_COIN])
            ->when(is_numeric($startTime), fn($query) => $query->where('created', '>=', $startTime))
            ->when(is_numeric($endTime), fn($query) => $query->where('created', '<=', $endTime));

        return [
            'total_count' => $query->count(),
            'amount_sum' => $query->sum('amount')
        ];
    }
}
